﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using MeizuSDK.Core;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 单行文本框控件
    /// </summary>
    public class TextBox : TextBoxBase
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiSingleLineEditWrapperCtor(OnFocusedFunc pFocused, OnLostFocusFunc pLostFocus);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetTip(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperAddString(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text, int pos);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperEnablePassWord(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperEnableZoomIn(IntPtr pObj, bool b);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperEnableUpdateDis(IntPtr pObj, bool b);

        [DllImport("MZFCWrapper.dll")]
        private static extern string UiSingleLineEditWrapperGetPassWord(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiSingleLineEditWrapperGetCursePos(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetCursePos(IntPtr pObj, int pos);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiSingleLineEditWrapperGetRightInvalid(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetRightInvalid(IntPtr pObj, int offset);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiSingleLineEditWrapperGetLeftInvalid(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetLeftInvalid(IntPtr pObj, int pos);

        [DllImport("MZFCWrapper.dll")]
        private static extern uint UiSingleLineEditWrapperGetEditStyle(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetEditStyle(IntPtr pObj, uint dwStyle);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetText(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetTip2(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text, bool bRightAlign);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiSingleLineEditWrapperSetSipMode(IntPtr pObj, uint dwMode, uint dwSipOffset);
        #endregion

        #region 常量
        /// <summary>
        /// 获取单行文本框默认宽度
        /// </summary>
        public const int WIDTH = MzMetrics.MZM_WIDTH_SINGLEEDIT_LONG;
        /// <summary>
        /// 获取单行文本框默认高度
        /// </summary>
        public const int HEIGHT = MzMetrics.MZM_HEIGHT_SINGLELINE_EDIT;
        /// <summary>
        /// 默认字体大小
        /// </summary>
        public const int DefaultFontSize = 24;
        #endregion

        #region 私有变量

        OnFocusedFunc pFocused;
        OnLostFocusFunc pLostFocus;

        private string tipText;
        private bool isTipAutoHide;
        private bool isTipRightAlign;
        private int leftInvalidOffset;
        private int rightInvalidOffset;
        private bool hasSetTip2;
        private bool autoSetTipWidth;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置一个标志，指示文本框提示消息是否在输入后自动消失
        /// </summary>
        public bool IsTipAutoHide
        {
            get
            {
                return this.isTipAutoHide;
            }
            set
            {
                if (value != this.isTipAutoHide)
                {
                    this.isTipAutoHide = value;

                    ResetTip(Tip);
                }
            }
        }
        /// <summary>
        /// 获取或设置一个标志，指示是否自动设置Tip的大小。如果设置为false，则必须指定对应方向上的InvalidOffset
        /// </summary>
        public bool AutoSetTipWidth
        {
            get { return this.autoSetTipWidth; }
            set{
                if(value!=this.autoSetTipWidth)
                {
                    this.autoSetTipWidth = value;
                    ResetTip(Tip);
                }
            }
        }
        /// <summary>
        /// 获取或设置一个标志，指示当IsTipAutoHide=false时，提示消息是否靠右显示
        /// </summary>
        public bool IsTipRightAlign
        {
            get
            {
                return this.isTipRightAlign;
            }
            set
            {
                if (value != this.isTipRightAlign)
                {
                    this.isTipRightAlign = value;

                    ResetTip(Tip);
                }
            }
        }
        /// <summary>
        /// 获取或设置设置文本框提示消息
        /// </summary>
        public override string Tip
        {
            get
            {
                return this.tipText;
            }
            set
            {
                if (value != this.tipText)
                {
                    this.tipText = value;

                    ResetTip(this.tipText);
                }
            }
        }
        /// <summary>
        /// 获取或设置编辑框的编辑模式
        /// </summary>
        public TextBoxEditStyle EditStyle
        {
            get
            {
                return (TextBoxEditStyle)UiSingleLineEditWrapperGetEditStyle(Handle);
            }
            set
            {
                UiSingleLineEditWrapperSetEditStyle(Handle, (uint)value);
            }
        }
        /// <summary>
        /// 获取或设置文本框左侧无效区域宽度
        /// </summary>
        public override int LeftInvalidOffset
        {
            get
            {
                return this.leftInvalidOffset;
            }
            set
            {
                UiSingleLineEditWrapperSetLeftInvalid(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置文本框右侧无效区域宽度
        /// </summary>
        public override int RightInvalidOffset
        {
            get
            {
                return this.rightInvalidOffset;
            }
            set
            {
                UiSingleLineEditWrapperSetRightInvalid(Handle, value);
            }
        }
        /// <summary>
        /// 获取或设置文本框显示的文本
        /// </summary>
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (value != base.Text)
                {
                    UiSingleLineEditWrapperSetText(Handle, value);
                }
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造单行文本框对象
        /// </summary>
        public TextBox()
        {
            pFocused = new OnFocusedFunc(InternalOnFocused);
            pLostFocus = new OnLostFocusFunc(InternalOnLostFocus);

            Handle = UiSingleLineEditWrapperCtor(pFocused,pLostFocus);
            SetID();
            Size = new Size(WIDTH, HEIGHT);

            isTipAutoHide = true;
            isTipRightAlign = false;
            leftInvalidOffset = UiSingleLineEditWrapperGetLeftInvalid(Handle);
            rightInvalidOffset = UiSingleLineEditWrapperGetRightInvalid(Handle);

            Font = new Font(FontFamily.GenericSansSerif, DefaultFontSize, FontStyle.Regular);
        }
        #endregion

        #region 函数
        /// <summary>
        /// 重设Tip字符串
        /// </summary>
        /// <param name="value"></param>
        private void ResetTip(string value)
        {
            if (IsTipAutoHide)
            {
                if (hasSetTip2)
                {
                    LeftInvalidOffset = this.leftInvalidOffset;
                    RightInvalidOffset = this.rightInvalidOffset;

                    hasSetTip2 = false;
                }

                UiSingleLineEditWrapperSetTip(Handle, value);
            }
            else
            {
                hasSetTip2 = true;

                if (!String.IsNullOrEmpty(value) && AutoSetTipWidth)
                {
                    using (Graphics g = Graphics.FromHdc(NativeMethods.GetDC(IntPtr.Zero)))
                    {
                        int offset = (int)g.MeasureString(value, Font).Width;

                        if (IsTipRightAlign)
                            UiSingleLineEditWrapperSetRightInvalid(Handle, offset);
                        else
                            UiSingleLineEditWrapperSetLeftInvalid(Handle, offset);
                    }
                }

                UiSingleLineEditWrapperSetTip2(Handle, value, IsTipRightAlign);
            }
        }
        /// <summary>
        /// 向文本框中指定位置插入文本
        /// </summary>
        /// <param name="text">要插入的文本</param>
        /// <param name="pos">文本的插入点。字符索引</param>
        /// <param name="update">插入完成后是否立刻显示</param>
        public void AddString(string text, int pos, bool update)
        {
            UiSingleLineEditWrapperEnableUpdateDis(Handle, update);

            UiSingleLineEditWrapperAddString(Handle, text, pos);
        }
        /// <summary>
        /// 设置文本框输入法模式
        /// </summary>
        /// <param name="sipMode">输入法模式</param>
        public void SetSipMode(SipMode sipMode)
        {
            SetSipMode(sipMode, SipHelper.DefaultSipOffset);
        }
        /// <summary>
        /// 设置文本框输入法模式
        /// </summary>
        /// <param name="sipMode">输入法模式</param>
        /// <param name="offset">输入法离屏幕底部的偏移高度,如果值为0xffffffff，采用系统默认值</param>
        public void SetSipMode(SipMode sipMode, uint offset)
        {
            uint mode = (uint)sipMode;

            UiSingleLineEditWrapperSetSipMode(Handle, mode, offset);
        }
        #endregion
    }
}
